package cn.com.aiton.educenter.service.impl;

import cn.com.aiton.educenter.entity.PestsQrcode;
import cn.com.aiton.educenter.mapper.PestsQrcodeMapper;
import cn.com.aiton.educenter.service.FileService;
import cn.com.aiton.educenter.service.PestsQrcodeService;
import cn.com.aiton.educenter.utils.ConstantPropertiesUtil;
import cn.com.aiton.educenter.utils.QrcodeUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实体，二维码 服务实现类
 * </p>
 *
 * @author testAuthor
 * @since 2021-03-13
 */
@Service
public class PestsQrcodeServiceImpl extends ServiceImpl<PestsQrcodeMapper, PestsQrcode> implements PestsQrcodeService {
    Log log = LogFactory.get();
    public static final String QRCODE = "qrcode";
    @Autowired
    FileService fileService;

    @Override
    public boolean batchTrap(String userid,int start, int limit,String type) throws Exception {
        DateTime now = DateTime.now();
        long time = now.getTime();

        String path = System.getProperty("user.dir");

        log.debug(path, Level.DEBUG);
        ThreadUtil.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                List<PestsQrcode> pestsQrcodes = new ArrayList<>();
                for (int i = start; i < limit; i++) {
                    PestsQrcode pq = new PestsQrcode();
                    String uuid = IdUtil.fastSimpleUUID();
                    String filename = QrcodeUtil.encode(ConstantPropertiesUtil.TRAP_QRCODE_URL + uuid, null, path + "/" + QRCODE + "/", uuid, true);
                    String filepath = path + "/" + QRCODE + "/" + filename;
                    String url = fileService.upload(new File(filepath), QRCODE);
                    pq.setCodeNumber(uuid);
                    pq.setCodeUrl(url);
                    pq.setCodeInt(i);
                    pq.setUserId(userid);
                    pq.setStype(type);
                    pq.setStime(DateTime.now());
                    pestsQrcodes.add(pq);
                }
                boolean flag = saveBatch(pestsQrcodes);
            }
        });

        return true;
    }

    @Override
    public boolean batch(String userid,int start, int limit,String type) throws Exception {
        DateTime now = DateTime.now();
        long time = now.getTime();

        String path = System.getProperty("user.dir");

        log.debug(path, Level.DEBUG);
        ThreadUtil.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                List<PestsQrcode> pestsQrcodes = new ArrayList<>();
                for (int i = start; i < limit; i++) {
                    PestsQrcode pq = new PestsQrcode();
                    String uuid = IdUtil.fastSimpleUUID();
                    String filename = QrcodeUtil.encode(ConstantPropertiesUtil.QRCODE_URL + uuid, null, path + "/" + QRCODE + "/", uuid, true);
                    String filepath = path + "/" + QRCODE + "/" + filename;
                    String url = fileService.upload(new File(filepath), QRCODE);
                    pq.setCodeNumber(uuid);
                    pq.setCodeUrl(url);
                    pq.setCodeInt(i);
                    pq.setUserId(userid);
                    pq.setStype(type);
                    pq.setStime(DateTime.now());
                    pestsQrcodes.add(pq);
                }
                boolean flag = saveBatch(pestsQrcodes);
            }
        });

        return true;
    }

    @Override
    public boolean initOne(String userid,int codeInit,String type) throws Exception {
        DateTime now = DateTime.now();
        long time = now.getTime();

        String uuid = IdUtil.fastSimpleUUID();
        String path = System.getProperty("user.dir");

        log.debug(path, Level.DEBUG);
        String filename = QrcodeUtil.encode(ConstantPropertiesUtil.QRCODE_URL + uuid, null, path + "/" + QRCODE + "/", uuid, true);
        String filepath = path + "/" + QRCODE + "/" + filename;
        String url = fileService.upload(new File(filepath), QRCODE);

        PestsQrcode pq = new PestsQrcode();
        pq.setCodeNumber(uuid);
        pq.setCodeUrl(url);
        pq.setUserId(userid);
        pq.setStime(DateTime.now());
        pq.setCodeInt(codeInit);
        pq.setStype(type);
        boolean flag = this.save(pq);
        return flag;
    }

    @Override
    public PestsQrcode getByQrcode(String codeNumber) {
        QueryWrapper<PestsQrcode> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(codeNumber)) {
            //构建条件
            wrapper.eq("code_number",codeNumber);

        }
        wrapper.last("limit 1");
        log.debug("codeNumber",codeNumber);
        PestsQrcode qrcode = this.getOne(wrapper,true);
        return qrcode;
    }
}
